var util = require('../utils/util');
var daoUtil = require('../utils/daoUtil');
var search = require('../controller/controller.search');
var async = require('async');
var stockUtil = require('../utils/stock');
module.exports = {


    keysearch: function (params, cb) {
        var that = this;
        var validate = daoUtil.validate(['key'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        var key = params.key;
        daoUtil.proxy(function(models){
            var sql = "select * from (" +
                " select industryname as rowId,industryname as val,'industry' as type from tb_industry where industryname like '%"+key+"%' " +
                " union all " +
                "select rowId,concat_ws('-',stockCode,name,industry) as val,'stock' as type from tb_dic_stock where name like '%"+key+"%' or stockCode like '%"+key+"%' or industry like '%"+key+"%' " +
                " union all " +
                " select rowId,concat_ws('-',name,phone)as val,'user' as type  from tb_user where name like '%"+key+"%' or phone like '%"+key+"%' " +
                ")res limit 0,10 ";
            models.search.query(sql,function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    searchList: function (params, cb) {
        var that = this;
        var validate = daoUtil.validate(['key','type'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        var key = params.key;
        var type = params.type;
        //晨会
        var mconference_sql = "" ;
        //电话
        var tel_sql = "";
        //策略
        var strategymeeting_sql = "";
        //调研
        var survey_sql = "";
        //路演
        var roadshow_sql = "";
        //金股
        var stockrecommend_sql = "";
        //股票池
        var stockpool_sql = "";
        //知识库
        var knowledgecenter_sql = "";
        daoUtil.proxy(function(models){
            if(type == 'user'){
                //晨会
                 mconference_sql = "select a.rowId,(select name from tb_user where rowId = '"+key+"') title,createdAt time ,corepoint as val from tb_mconference a where createuid = '"+key+"' and status = 1 order by createdAt desc " ;
                //电话
                 tel_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title,starttime as time ,content as val,'tel' as type   from tb_teleconference a  where createuid = '"+key+"' order by createdAt desc ";
                //策略
                 strategymeeting_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'strategymeeting' as type from tb_strategymeeting a  where createuid = '"+key+"' order by createdAt desc";
                //调研
                 survey_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'surveying' as type from tb_surveying a  where createuid = '"+key+"' order by createdAt desc";
                //路演
                 roadshow_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'roadshow' as type from tb_roadshow a  where createuid = '"+key+"' order by createdAt desc";
                //金股
                 stockrecommend_sql = "select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user where rowId = '"+key+"'),'目标价',expectprice)) as val,'stockrecommend' as type from tb_stockrecommend a  where createuid = '"+key+"' and isrecommend = 1 and enable = 1 order by createdAt desc";
                //股票池
                 stockpool_sql = "select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user  where rowId = '"+key+"'),'目标价',expectprice)) as val,'stockpool' as type from tb_stockrecommend a  where createuid = '"+key+"' and isrecommend = 0 and enable = 1 order by createdAt desc";
                //知识库
                knowledgecenter_sql = "select a.rowId,a.tag,content ,a.createdAt as time   from tb_knowledgecenter a where (a.tag like '%"+key+"%' or a.content like '%"+key+"%')   order by a.createdAt desc";


            }else if(type == 'stock'){
                //晨会
                 mconference_sql = "select a.rowId,(select name from tb_user where rowId = a.createuid) title,a.createdAt time ,corepoint as val from tb_mconference a left join tb_stockrecommend b on a.rowId = b.actionid where b.stockid = '"+key+"' and a.status = 1 and b.enable = 1 group by a.rowId order by a.createdAt desc " ;
                //电话
                 tel_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title,starttime as time ,content as val,'tel' as type   from tb_teleconference a  where company = '"+key+"' order by a.createdAt desc ";
                //策略
                 strategymeeting_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'strategymeeting' as type from tb_strategymeeting a  where company = '"+key+"' order by a.createdAt desc";
                //调研
                 survey_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'surveying' as type from tb_surveying a  where company = '"+key+"' order by createdAt desc";
                //路演
                 roadshow_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'roadshow' as type from tb_roadshow a  where companyname = '"+key+"' order by createdAt desc";
                //金股
                 stockrecommend_sql = "select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user where rowId = a.createuid),'目标价',expectprice)) as val,'stockrecommend' as type from tb_stockrecommend a  where stockid = '"+key+"' and isrecommend = 1 and enable = 1 order by createdAt desc";
                //股票池
                 stockpool_sql = "select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user  where rowId = a.createuid),'目标价',expectprice)) as val,'stockpool' as type from tb_stockrecommend a  where stockid = '"+key+"' and isrecommend = 0 and enable = 1 order by createdAt desc";
                //知识库
                knowledgecenter_sql = "select a.rowId,a.tag,content ,a.createdAt as time   from tb_knowledgecenter a where (a.tag like '%"+key+"%' or a.content like '%"+key+"%')   order by a.createdAt desc";

            }else if(type == 'keyLike'){
                //晨会
                 mconference_sql = "select a.rowId,(select name from tb_user where rowId = a.createuid) title,a.createdAt time ,corepoint as val from tb_mconference a left join tb_comment b on a.rowId = b.mconference and b.enable = 1 where (a.corepoint like  '%"+key+"%' or b.context like '%"+key+"%' ) and a.status = 1  group by a.rowId order by a.createdAt desc " ;
                //电话
                 tel_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title,starttime as time ,content as val,'tel' as type   from tb_teleconference a  where content like  '%"+key+"%' order by a.createdAt desc ";
                //策略
                 strategymeeting_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'strategymeeting' as type from tb_strategymeeting a  where content like  '%"+key+"%' order by a.createdAt desc";
                //调研
                 survey_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'surveying' as type from tb_surveying a  where content like  '%"+key+"%' order by createdAt desc";
                //路演
                 roadshow_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'roadshow' as type from tb_roadshow a  where content like  '%"+key+"%' order by createdAt desc";
                //金股
                 stockrecommend_sql = "select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,a.createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user where rowId = a.createuid),'目标价',expectprice)) as val,'stockrecommend' as type from tb_stockrecommend a  left join tb_comment b on a.rowId = b.mconference and b.enable = 1 where (a.reason like '%"+key+"%' or b.context like '%"+key+"%') and isrecommend = 1   group by a.rowId order by a.createdAt desc";
                //股票池
                 stockpool_sql = "select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,a.createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user  where rowId = a.createuid),'目标价',expectprice)) as val,'stockpool' as type from tb_stockrecommend a left join tb_comment b on a.rowId = b.mconference and b.enable = 1 where (a.reason like '%"+key+"%' or b.context like '%"+key+"%') and isrecommend = 0 and a.enable = 1  group by a.rowId  order by a.createdAt desc";
                //知识库
                knowledgecenter_sql = "select a.rowId,a.tag,content ,a.createdAt as time   from tb_knowledgecenter a where (a.tag like '%"+key+"%' or a.content like '%"+key+"%')   order by a.createdAt desc";

            }else if(type == 'industry'){
                //晨会
                 mconference_sql = "select a.rowId,(select name from tb_user where rowId = a.createuid) title,a.createdAt time ,corepoint as val from tb_mconference a  where a.industry = '"+key+"' and a.status = 1  group by a.rowId order by a.createdAt desc " ;
                //电话
                 tel_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title,starttime as time ,content as val,'tel' as type   from tb_teleconference a  where industry = '"+key+"' order by a.createdAt desc ";
                //策略
                 strategymeeting_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'strategymeeting' as type from tb_strategymeeting a  where industry = '"+key+"' order by a.createdAt desc";
                //调研
                 survey_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'surveying' as type from tb_surveying a  where industry = '"+key+"' order by createdAt desc";
                //路演
                 roadshow_sql = "select a.rowId,(select name from tb_seller where rowId = a.seller)title ,starttime as time  ,content as val,'roadshow' as type from tb_roadshow a  where industry = '"+key+"' order by createdAt desc";
                //金股
                stockrecommend_sql = "select * from (select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,a.createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user where rowId = a.createuid),'目标价',expectprice)) as val,'stockrecommend' as type,(select industry from tb_dic_stock where rowId = a.stockid)as industry from tb_stockrecommend a   where  isrecommend = 1   group by a.rowId order by a.createdAt desc ) b where b.industry =  '"+key+"'";
                //股票池
                stockpool_sql = "select * from (select a.rowId,a.importprice,(select concat_ws('-',stockCode,name,industry) from tb_dic_stock where rowId = a.stockid)title ,a.createdAt as time  ,( concat_ws(' ',(select name from tb_seller where rowId = a.seller),(select name from tb_user where rowId = a.createuid),'目标价',expectprice)) as val,'stockrecommend' as type,(select industry from tb_dic_stock where rowId = a.stockid)as industry from tb_stockrecommend a   where  isrecommend = 0   group by a.rowId order by a.createdAt desc ) b where b.industry =  '"+key+"'";
                //知识库
                knowledgecenter_sql = "select a.rowId,a.tag,content ,a.createdAt as time   from tb_knowledgecenter a where (a.tag like '%"+key+"%' or a.content like '%"+key+"%')   order by a.createdAt desc";

            }
            async.parallel({
                mconferenceList:function(done){
                    models.search.query(mconference_sql,function (err, data) {
                        if (err || !data) {
                            console.error(err);
                            done(util.getSysError());
                        }else{
                            done(null, util.getSuccessRes(data));
                        }
                    });
                },
                telList:function(done){
                    models.search.query(tel_sql,function (err, data) {
                        if (err || !data) {
                            console.error(err);
                            done(util.getSysError());
                        }else{
                            done(null, util.getSuccessRes(data));
                        }
                    });
                },
                strategymeetingList:function(done){
                    models.search.query(strategymeeting_sql,function (err, data) {
                        if (err || !data) {
                            console.error(err);
                            done(util.getSysError());
                        }else{
                            done(null, util.getSuccessRes(data));
                        }
                    });
                },
                surveyList:function(done){
                    models.search.query(survey_sql,function (err, data) {
                        if (err || !data) {
                            console.error(err);
                            done(util.getSysError());
                        }else{
                            done(null, util.getSuccessRes(data));
                        }
                    });
                },
                knowledgecenterList:function(done){
                    models.search.query(knowledgecenter_sql,function (err, data) {
                        if (err || !data) {
                            console.error(err);
                            done(util.getSysError());
                        }else{
                            done(null, util.getSuccessRes(data));
                        }
                    });
                },
                roadshowList:function(done){
                    models.search.query(roadshow_sql,function (err, data) {
                        if (err || !data) {
                            console.error(err);
                            done(util.getSysError());
                        }else{
                            done(null, util.getSuccessRes(data));
                        }
                    });
                },
                stockrecommendList:function(done){
                    if(stockrecommend_sql == ""){
                        done(null,util.getSuccessRes([]));
                    }else{
                        models.search.query(stockrecommend_sql,function (err, data) {
                            if (err || !data) {
                                console.error(err);
                                done(util.getSysError());
                            }else{
                                var stockCodes = [];
                                for(stock of data){
                                    stockCodes.push(stock.title.substring(0,stock.title.indexOf("-")));
                                }
                                if(stockCodes.length == 0){
                                    done(null,util.getSuccessRes(data));
                                }else{
                                    stockUtil.getRealtimeListPrice(stockCodes,function (err,res) {
                                        var i = 0;
                                        for(stock of data){
                                            stock.realPrice = res[i];
                                            i++;
                                        }
                                        done(null,util.getSuccessRes(data));
                                    });
                                }
                            }
                        });
                    }

                },
                stockpoolList:function(done){
                    if(stockpool_sql == ""){
                        done(null,util.getSuccessRes([]));
                    }else{
                        models.search.query(stockpool_sql,function (err, data) {
                            if (err || !data) {
                                console.error(err);
                                done(util.getSysError());
                            }else{
                                var stockCodes = [];
                                for(stock of data){
                                    stockCodes.push(stock.title.substring(0,stock.title.indexOf("-")));
                                }
                                if(stockCodes.length == 0){
                                    done(null,util.getSuccessRes(data));
                                }else {
                                    stockUtil.getRealtimeListPrice(stockCodes, function (err, res) {
                                        var i = 0;
                                        for (stock of data) {
                                            stock.realPrice = res[i];
                                            i++;
                                        }
                                        done(null, util.getSuccessRes(data));
                                    });
                                }
                            }
                        });
                    }

                }
            },function(error,results){
                cb(null,results);
            });

        },cb);
    }
};